<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/1/9
 * Time: 下午5:30
 */
namespace app\super\controller;

use think\Db;
//use app\super\validate\MoneyValidate;
class MoneyController extends CommonController{
    /**
     * getMoneyDate 获取财务管理数据
     * @return 展示财务管理页面
     */
    public function getMoneyDate(){
        $where = '';
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        /*关键字*/
        if($_POST['keyword']!=''){
            $where['u.user_username|u.user_phone'] = $_POST['keyword'];
        }
        /*支付方式*/
        if($_POST['method_payment']!=''){
            $where['ub.bill_pay_type'] = $_POST['method_payment'];
        }
        /*项目类型*/
        if($_POST['project_type']!=''){
            $where['ub.bill_money_type'] = $_POST['project_type'];
        }
        /*付款类型*/
        if($_POST['payment_type']!=''){
            $where['ub.bill_state'] = $_POST['payment_type'];
        }
        /*支付状态*/
        if($_POST['payment_state']!=''){
            $where['ub.bill_type'] = $_POST['payment_state'];
        }

        if($_POST['end']!='' && $_POST['start']!=''){
            $res = $this->betweenDate($where,$rows,$page,$_POST['start'],$_POST['end']);
        }else{
            if ($_POST['start'] != '') {//开始时间
                $where['ub.bill_time'] = array('>=', $_POST['start']);
            }
            if ($_POST['end'] != '') {//结束时间
                $where['ub.bill_time'] = array('<=', $_POST['end']);
            }
            $res = $this->Date($where,$rows,$page);
        }

//        $res = $this->Date($where,$rows,$page);
        $total = $this->totalDate($where);

        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * getFinDate 获取今日|当月财务数据
     * @return 展示今日|当月财务数据页面
     */
    public function getFinDate(){
        if(input('type')==1){
            //获取当天开始与结束时间间隔
            $start = date("Y-m-d");
            $end = date("Y-m-d",strtotime("+1 day"));
        }elseif(input('type')==2){
            //获取当月第一天
            $start = date('Y-m-01', strtotime(date("Y-m-d")));
            //获取当月最后一天
            $end = date('Y-m-d', strtotime("$start +1 month -1 day")) .'23'.":".'59'.":".'59';
        }
        $data = $this->finDate($start,$end);
        /*充值次数*/
        $i = 0;
        /*支出次数*/
        $j = 0;
        /*支出金额*/
        $spend = 0;
        /*收入金额*/
        $income = 0;
        foreach($data as $key=>$vo){
            if($vo['bill_money_type']==2 && $vo['bill_type']==1){
                $i++;
            }

            if($vo['bill_state']==2 && $vo['bill_type']==1 && $vo['bill_money_type']!==1){
                $j++;
                $spend = $spend + $vo['bill_money'];
            }elseif($vo['bill_state']==1 && $vo['bill_type']==1){
                $income = $income + $vo['bill_money'];
            }
        }
        $this->assign('top_up',$i);
        $this->assign('spend_num',$j);
        $this->assign('spend',$spend);
        $this->assign('income',$income);
        return $this->fetch('money/todayMoneyInfo');
    }

    /**
     * Date 查询财务表信息
     * @param $where 查询条件，$rows $page 分页数据。
     * @return $data 返回的多表查询数据
     */
    protected function Date($where,$rows,$page){
        $data = Db::table('up_u_bill')->alias('ub')
            ->where($where)
            ->join('up_u_user u','u.user_id=ub.user_id','left')
            ->field('u.user_username,u.user_phone,ub.bill_money_type,ub.bill_state,ub.bill_money,ub.bill_pay_type,ub.bill_time,ub.bill_type')
            ->limit($rows * ($page - 1), $rows)
            ->order('ub.bill_time desc')
            ->select();
        return $data;
    }

    /**
     * betweenDate 时间区间查询用户表信息
     * @param $where 查询条件，$rows $page 分页数据 $s 起始时间 $e 结束时间。
     * @return $data 返回的多表查询数据
     */
    protected function betweenDate($where,$rows,$page,$s,$e){
        $data = Db::table('up_u_bill')->alias('ub')
            ->where($where)
            ->whereTime('ub.bill_time','between',[$s,$e])
            ->join('up_u_user u','u.user_id=ub.user_id','left')
            ->field('u.user_username,u.user_phone,ub.bill_money_type,ub.bill_state,ub.bill_money,ub.bill_pay_type,ub.bill_time,ub.bill_type')
            ->limit($rows * ($page - 1), $rows)
            ->order('ub.bill_time desc')
            ->select();
        return $data;
    }


    /**
     * totalDate 查询财务表信息
     * @param $where 查询条件。
     * @return $data 返回的多表查询数据
     */
    protected  function totalDate($where){
        $data = Db::table('up_u_bill')->alias('ub')
            ->where($where)
            ->join('up_u_user u','u.user_id=ub.user_id','left')
            ->count();
        return $data;
    }

    /**
     * finDate 查询财务表信息
     * @return $data 返回表查询数据
     */
    protected  function finDate($start,$end){
        $data = Db::table('up_u_bill')
            ->whereTime('bill_time', 'between', [$start, $end])
            ->field('bill_money,bill_money_type,bill_type,bill_state')
            ->select();
        return $data;
    }

}